package com.phkj.qdh.ui.fragment.baseinspect

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.hjq.toast.ToastUtils
import com.phkj.qdh.base.BaseVM
import com.phkj.qdh.bean.BaseInspectBean
import com.phkj.qdh.net.msg
import com.phkj.qdh.repository.InspectRepository
import com.phkj.qdh.utils.requestIO
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collectLatest

class BaseInspectVM(private val inspect: InspectRepository):BaseVM() {

    private val _baseInspectType = MutableSharedFlow<ArrayList<BaseInspectBean>>()
    val baseInspectType get() = _baseInspectType
    fun getBaseInspectType() {
        requestIO {
            inspect.getBaseInspectType().catch { cause ->
                ToastUtils.show(cause.msg)
            }.collectLatest {
                _baseInspectType.emit(it)
            }
        }
    }


    private val _baseInspectSave = MutableSharedFlow<String>()
    val baseInspectSave get() = _baseInspectSave
    fun baseInspectSave(map: HashMap<String,Any?>) {
        requestIO {
            inspect.baseInspectSave(map).catch { cause ->
                ToastUtils.show(cause.msg)
            }.collectLatest {
                _baseInspectSave.emit(it)
            }
        }
    }

}


fun getBaseInspectVMProvider() = BaseInspectVMProvider(InspectRepository)

class BaseInspectVMProvider(private val inspect: InspectRepository):ViewModelProvider.Factory{
    override fun <T : ViewModel> create(modelClass: Class<T>): T {
        return BaseInspectVM(inspect) as T
    }
}